﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.BBS
{
    public class News : BoardBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected GroupNewsInfo NewsData;
        protected const string Alias = "优医网友";
        protected int CommentCount;
        protected NewsComment NewsComment = new NewsComment();
        protected List<GroupNewsInfo> NewsList = new List<GroupNewsInfo>();
        protected List<NewsComment> NewsCommentList = new List<NewsComment>();
        

        protected void HandleCommon()
        {
            this.AddLocation("版块新闻列表", "/BBS/NewsList.aspx?gid=" + GroupData.Id);

            int id = RequestTools.GetInt("id", 0);
            if (id <= 0)
            {
                this.AddErrLine("无效的参数 id ！");
                return;
            }
            NewsList = GroupNewsBLL.Instance.GetLattestNews(GroupData.Id, 10);
            NewsData = GroupNewsBLL.Instance.GetGroupNewsInfo(id);
            NewsCommentList = GroupNewsBLL.Instance.GetGroupNewsCommentList(id);
            CommentCount = GroupNewsBLL.Instance.GetNewsCommentCount(id);
            if (NewsData.Id <= 0)
            {
                this.AddErrLine("该新闻不存在！");
                return;
            }
            this.SetTitle(NewsData.Title);
            this.AddLocation(NewsData.Title.Length>20?NewsData.Title.Substring(0,20):NewsData.Title, "/BBS/News.aspx?gid=" + GroupData.Id + "&id=" + NewsData.Id);
        }

        protected void HandlePost()
        {
            string userName = RequestTools.GetFormString("UserName");
            string password = Utils.MD5(RequestTools.GetFormString("Password"), 16);
            string anonymousflag = RequestTools.GetFormString("anonymous");
            if ((!string.IsNullOrEmpty(userName)) && (!string.IsNullOrEmpty(password)))
            {
                UserData = UserBLL.Instance.CheckUserLogin(userName, password);
                if (UserData.Id > 0)
                {
                    CookiesTools.WriteUserInfoCookie(userName, password, UserData.Id, 5256000);
                }
            }
            string userComment = RequestTools.GetFormString("textareaComment");
            if (!string.IsNullOrEmpty(userComment))
            {
                NewsComment.CommentData = userComment;
            }
            else
            {
                return ;
            }
            NewsComment.NewsID = NewsData.Id;
            if ((!string.IsNullOrEmpty(UserData.UserName)) && (anonymousflag != "on"))
            {
                NewsComment.Alias = UserData.UserName;
                NewsComment.UserID = UserData.Id;               ;
            }
            NewsComment.InsertDateTime = DateTime.Now;
            
            GroupNewsBLL.Instance.AddGroupNewsComment(NewsComment);
            int id = RequestTools.GetInt("id", 0);
            if (id <= 0)
            {
                this.AddErrLine("无效的参数 id ！");
                return;
            }
            NewsList = GroupNewsBLL.Instance.GetLattestNews(GroupData.Id, 10);
            NewsData = GroupNewsBLL.Instance.GetGroupNewsInfo(id);
            NewsCommentList = GroupNewsBLL.Instance.GetGroupNewsCommentList(id);
            CommentCount = GroupNewsBLL.Instance.GetNewsCommentCount(id);
            if (NewsData.Id <= 0)
            {
                this.AddErrLine("该新闻不存在！");
                return;
            }
        }

        protected void HandleGet()
        {

           
        }
    }
}